﻿using System.Threading.Channels;
using Letter.IO.Channels;

namespace ChannelTest
{
    class SystemChannel
    {
        private static Program E = new Program();



        private static IChannel<Program> channel;
        private static int total_count = 0;
        private static int current_count = 0;


        public static async Task StartAsync()
        {
            channel = ChannelProvider.Create<Program>(true);
            channel.Reader.SetListener(StartReadAsync1);
            
            while (true)
            {
                var str = Console.ReadLine();
                if (str == "out")
                {
                    channel.Writer.Complete();
                    Console.WriteLine(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>out");
                }
                else if (str == "info")
                {
                    Console.WriteLine(">>>" + channel.Reader.Count);
                }
                else
                {
                    int count = 100_000;
            
                    total_count = count;
                    current_count = 0;
                    for (int i = 0; i < count; i++)
                    {
                        if (await channel.Writer.TryWriteAsync(E))
                        {
                        }
                    }
                }
            }
        }

        private static int count = 0;
        private static ValueTask StartReadAsync1(Program t1)
        {
            count++;
            Console.WriteLine(count);
            return ValueTask.CompletedTask;
        }

        private static async Task StartReadAsync()
        {
            // while (await channel.Reader.WaitToReadAsync())
            // {
            //     if (channel.Reader.TryRead(out var number))
            //     {
            //         current_count++;
            //         if (total_count == current_count)
            //         {
            //             Console.WriteLine("完成::" + DateTime.Now);
            //         }
            //     }
            // }
            //
            // Console.WriteLine("结束");
        }
    }
}
